package com.example.memo;

import android.content.Context;
import android.content.Intent;

import android.database.sqlite.SQLiteDatabase;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.example.memo.Bean.noteBean;
import com.example.memo.Services.FilterListener;
import com.example.memo.Services.ResultListener;
import com.example.memo.db.MysqlOpenHelper;

import java.util.List;

public class Myadapter extends BaseAdapter implements  ResultListener{

    private List<noteBean> data;

    private ResultListener resultListener;


    public Myadapter(List<noteBean> data, Context context,ResultListener resultListener) {
        this.data = data;
        this.context = context;
        this.resultListener=resultListener;
    }


    public void setData(List<noteBean> data) {
            this.data = data;
            notifyDataSetChanged();
    }

    private Context context;

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        //使用viewHolder 绑定每个item中的控件id
        ViewHolder viewHolder;
        //每个item都要创建，
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.simple_item, viewGroup, false);
            //绑定item下的控件id
            viewHolder = new ViewHolder();
            viewHolder.imgid = view.findViewById(R.id.imgeOne);
            viewHolder.bt = view.findViewById(R.id.title);
            viewHolder.time = view.findViewById(R.id.subtitle);
            viewHolder.neirong = view.findViewById(R.id.content);
            view.setTag(viewHolder);
            //绑定对应的id
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        if(data.size()!=0){
            viewHolder.bt.setText(data.get(position).getBt());
            viewHolder.neirong.setText(data.get(position).getNeirong());
            //viewHolder.imgid.setImageAlpha(Integer.parseInt(data.get(position).getImg()));
            viewHolder.time.setText(data.get(position).getCreate_time());
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, addNoteFragment.class);
                intent.putExtra("id", data.get(position).getId());
                context.startActivity(intent);
            }
        });
        view.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                MysqlOpenHelper mysqlOpenHelper = new MysqlOpenHelper(context);
                SQLiteDatabase sqLiteDatabase = mysqlOpenHelper.getWritableDatabase();
                sqLiteDatabase.delete("note_table", "id=?", new String[]{data.get(position).getId()});
                Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
                data.remove(position);
                notifyDataSetChanged();
                resultListener.getNoteData(data);
                return true;
            }
        });
        return view;
    }

    @Override
    public void getNoteData(List<noteBean> lists) {

    }
    //View需要使用的子控件
    private final class ViewHolder {
        ImageView imgid;
        TextView bt;
        TextView time;
        TextView neirong;

    }


}
